<?php

use GatewayWorker\Lib\Gateway;

/**
 * Here is your custom functions.
 * @param $clientId
 * @param array $msg
 */

/**
 * @param $clientId
 * @param string $msg
 * @param array $data
 * @author xqiaochina@163.com
 * 向个人发送消息
 */
function send($clientId, string $msg = '请求成功', array $data = []): void
{
    Gateway::sendToClient($clientId, json_encode([
        'msg' => $msg,
        'data' => $data,
    ], JSON_UNESCAPED_UNICODE));
}

/**
 * @param $roomId
 * @param string $msg
 * @param array $data
 * @author xqiaochina@163.com
 * 向群组发送消息
 */
function send_to_group($roomId, string $msg = '请求成功', array $data = []): void
{
    Gateway::sendToGroup($roomId, json_encode([
        'msg' => $msg,
        'data' => $data,
    ], JSON_UNESCAPED_UNICODE));
}

function outJson($code, $msg, $data = [])
{
    $out['code'] = $code;
    $out['msg'] = $msg;
    $out['data'] = $data;
    return json($out);
}

function isItem($item_key)
{
    if ($item_key > 211000100) {
        return false;
    }
    return true;
}

function getItems($file_name)
{
    $path = app_path() . '/tables/' . $file_name;
    $file_content = file_get_contents($path);
    $file_content = json_decode($file_content, true);

    $arrs = $file_content['table_data']['data'];
    $keys = array_shift($arrs);
    $column_types = array_shift($arrs);
    $temp_data = [];
    foreach ($arrs as $index => $items) {
        $temp_arr = [];
        foreach ($items as $key => $item) {
            $temp_arr[$keys[$key]] = $item;
        }
        $temp_data[] = $temp_arr;
    }
    $temp_data = array_column($temp_data, null, 'key');
    return $temp_data;
}

function getTodayStartEndTimestamp()
{
    $s = strtotime(date('Y-m-d') . '00:00:00');
    $l = strtotime(date('Y-m-d') . '23:59:59');
    return [$s, $l];
}
